package org.elephas.webapp.backend.page;

import org.apache.wicket.PageParameters;
import org.apache.wicket.ResourceReference;
import org.apache.wicket.ajax.AjaxRequestTarget;
import org.apache.wicket.ajax.markup.html.AjaxLink;
import org.apache.wicket.markup.html.IHeaderContributor;
import org.apache.wicket.markup.html.IHeaderResponse;
import org.apache.wicket.markup.html.WebMarkupContainer;
import org.apache.wicket.markup.html.WebPage;
import org.apache.wicket.markup.html.panel.Panel;
import org.elephas.webapp.backend.component.management.ApplicationAdminPanel;
import org.elephas.webapp.backend.component.management.UserManagementPanel;

public class BaseAdminPage extends WebPage implements IHeaderContributor {
	private static final long serialVersionUID = 1L;

	private static final String ADMIN_CONTENT_ID = "adminContent";
	private static final ResourceReference CSS = new ResourceReference(
			BaseAdminPage.class, "res/style.css");

	public BaseAdminPage(PageParameters params) {
		add(new ManagementMenuItem("appManagementLink") {
			private static final long serialVersionUID = 1L;

			@Override
			protected Panel newManagementPanel(String id) {
				return new ApplicationAdminPanel(id);
			}

		});
		
		add(new ManagementMenuItem("userManagementLink") {
			private static final long serialVersionUID = 1L;

			@Override
			protected Panel newManagementPanel(String id) {
				return new UserManagementPanel(id, null);
			}
		});
		
		add(new WebMarkupContainer(ADMIN_CONTENT_ID).setOutputMarkupId(true));
	}
	
	public void renderHead(IHeaderResponse response) {
		response.renderCSSReference(CSS);
	}
	
	private abstract class ManagementMenuItem extends AjaxLink {
		private static final long serialVersionUID = 1L;

		private ManagementMenuItem(String id) {
			super(id);
		}

		@Override
		public void onClick(AjaxRequestTarget target) {
			Panel panel = newManagementPanel(ADMIN_CONTENT_ID);
			BaseAdminPage.this.replace(panel);
			target.addComponent(panel.setOutputMarkupId(true));
		}

		protected abstract Panel newManagementPanel(String id);
	}
}
